<?php

namespace App\Models;

class MoneySubCategory extends Model
{
    protected $table = 'money_sub_categories';

    protected $fillable = [
        'book_id',     // 新增账本ID字段
        'category_id',
        'icon',
        'name',
        'sort',
        'description',
    ];

    /**
     * 获取该子分类所属的账本
     */
    public function book()
    {
        return $this->belongsTo(MoneyBook::class, 'book_id');
    }

    /**
     * 获取该子分类所属的主分类
     */
    public function category()
    {
        return $this->belongsTo(MoneyCategory::class, 'category_id');
    }

    /**
     * 获取该子分类下的所有交易记录
     */
    public function transactions()
    {
        return $this->hasMany(MoneyTransaction::class, 'sub_category_id');
    }
}
